﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

public class GameVideoUI : MonoBehaviour {

    public VideoPlayer videoPlayer = null;
    public AudioSource audioSource = null;
    public GameObject videoUI = null;
    public GameObject overUI = null;
    public GameObject ingUI = null;

    // Use this for initialization
    void Awake () {
        videoPlayer.prepareCompleted += beginPlayVideo;
        videoPlayer.started += OnVideoPlayerStarted;
        videoPlayer.loopPointReached += OnLoopPointReached;
 //       videoPlayer.frameReady += OnVideoPlayerFrameReady;
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    void beginPlayVideo(VideoPlayer vp)
    {
        vp.Play();
    }

    void OnLoopPointReached(VideoPlayer vp)
    {
        // 播放完了
        OpenVideoUI();
    }

    void OnVideoPlayerStarted(VideoPlayer vp)
    {
        Loading.GetInstance().EndLoading();
    }

    void OnVideoPlayerFrameReady(VideoPlayer vp, long f)
    {
        Debug.Log(f);
        if ( f == 2)
            Loading.GetInstance().EndLoading();
    }


    public void StartVideo(string urlVideo)
    {


        if (string.IsNullOrEmpty(urlVideo))
        {
            BaseGame.nowGame.isVideoFinished = true;
            gameObject.SetActive(false);
            return;
        }

        Loading.GetInstance().StartLoading();

        gameObject.SetActive(true);


        string newUrl = ResLoader.Url2LocalPath(urlVideo);
        videoPlayer.url = newUrl;
        videoPlayer.Prepare();




        //        gameUI.GetInstance().btnStartGame.SetActive(false);
        gameUI.GetInstance().gameObject.SetActive(false);
                videoUI.SetActive(false);
    }

    public void OpenVideoUI()
    {
        videoUI.SetActive(true);
        if (videoPlayer.frameCount - (ulong)videoPlayer.frame > 1 )
        {
            ingUI.SetActive(true);
            overUI.SetActive(false);

            videoPlayer.Pause();
        }
        else
        {
            ingUI.SetActive(false);
            overUI.SetActive(true);

        }
    }

    public void OnBack()
    {



        // 返回课程界面,退出游戏
        BaseGame.nowGame.CloseGame();

    }

    public void OnBackFromVideo()
    {
        // 关闭界面和视频
        videoPlayer.Stop();
        gameObject.SetActive(false);

        BaseGame.nowGame.CloseGame();

        lessonUI.GetInstance().gameObject.SetActive(true);
        lessonUI.GetInstance().ShowLessonInfo();
    }

    public void OnStartGame()
    {
        BaseGame.nowGame.isVideoFinished = true;
        gameObject.SetActive(false);

       gameUI.GetInstance().gameObject.SetActive(true);
        gameUI.GetInstance().chooseSpeed.SetActive(true);
//        gameUI.GetInstance().OnBtnStartGame();
    }

    public void OnGoon()
    {
        videoUI.SetActive(false);
        videoPlayer.Play();
    }

    public void OnReplay()
    {
        videoUI.SetActive(false);
        videoPlayer.Stop();
        videoPlayer.Play();
    }
}
